package bb.lanxing.activity.sport;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import bb.lanxing.R;
import bb.lanxing.adapter.WorkoutMergeListAdapter;
import bb.lanxing.model.database.Workout;
import bb.lanxing.mvp.presetner.WorkoutMergeListPresenterImpl;
import bb.lanxing.mvp.presetner.i.IWorkoutMergeListPresenter;
import bb.lanxing.activity.base.BaseViewActivity;
import bb.lanxing.mvp.view.i.IWorkoutMergeListView;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.RecyclerViewItemClickHelper;
import bb.lanxing.util.ui.LinearItemDecoration;
import bb.lanxing.view.BiciAlertDialogBuilder;
import java.lang.ref.WeakReference;

public class WorkoutMergeListActivity extends BaseViewActivity implements
        IWorkoutMergeListView, View.OnClickListener, RecyclerViewItemClickHelper.OnItemClickListener {
    private static final int REQUEST_MERGE_PREVIEW = 2;
    private TextView downloadBtn;
    private RecyclerViewItemClickHelper eventHelper;
    private IWorkoutMergeListPresenter presenter;
    private TextView previewBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long[] longArrayExtra = getIntent().getLongArrayExtra("workout_id_arrays");
        if (longArrayExtra == null || longArrayExtra.length == 0) {
            toast(R.string.params_error);
            finish();
            return;
        }
        this.presenter = new WorkoutMergeListPresenterImpl(longArrayExtra, this);
        setContentView(R.layout.activity_workout_merge_list);
        initViews();
    }

    private void initViews() {
        setupActionBar(true);
        setTitle(R.string.workout_merge_list_title);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new LinearItemDecoration(ContextCompat.getColor(this, R.color.common_back), DensityUtil.dp2px(this, 6.0f)));
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(this.presenter));
        itemTouchHelper.attachToRecyclerView(recyclerView);
        recyclerView.setAdapter(new WorkoutMergeListAdapter(this.presenter, itemTouchHelper));
        recyclerView.addItemDecoration(itemTouchHelper);
        this.previewBtn = (TextView) findViewById(R.id.btn_preview);
        this.downloadBtn = (TextView) findViewById(R.id.btn_download);
        RecyclerViewItemClickHelper recyclerViewItemClickHelper = new RecyclerViewItemClickHelper(this, null);
        this.eventHelper = recyclerViewItemClickHelper;
        recyclerViewItemClickHelper.attachToRecyclerView(recyclerView);
        boolean isDownloaded = this.presenter.isDownloaded();
        this.previewBtn.setVisibility(isDownloaded ? View.VISIBLE : View.GONE);
        this.downloadBtn.setVisibility(isDownloaded ? View.GONE : View.VISIBLE);
        this.previewBtn.setOnClickListener(this);
        this.downloadBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btn_download) {
            this.presenter.download();
        } else if (id == R.id.btn_preview) {
            startActivityForResult(new Intent(this, WorkoutMergePreviewActivity.class)
                    .putExtra("workout_id_arrays", this.presenter.getWorkoutIds()), 2);
        }
    }

    @Override
    public void onProgressUpdate(int index, int total) {
        this.downloadBtn.setText(getString(R.string.workout_merge_list_download_progress, index, total));
    }

    @Override
    public void onDownloadStarted() {
        this.downloadBtn.setEnabled(false);
    }

    @Override
    public void onDownloadCompleted() {
        toast(R.string.toast_download_finish);
        this.downloadBtn.setEnabled(false);
        this.downloadBtn.setVisibility(View.GONE);
        this.previewBtn.setEnabled(true);
        this.previewBtn.setVisibility(View.VISIBLE);
    }

    @Override
    public void onDownloadFailed() {
        toast(R.string.toast_download_failed);
        this.downloadBtn.setText(R.string.workout_merge_list_download_start);
        this.downloadBtn.setEnabled(true);
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder) {
        Workout item = this.presenter.getItem(viewHolder.getAdapterPosition());
        if (item != null) {
            Intent intent = new Intent(this, SportRecordDetailActivity.class);
            intent.putExtra("workoutId", item.getId());
            startActivity(intent);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        new BiciAlertDialogBuilder(this)
                .setTitle(R.string.workout_merge_list_title_back_to_select)
                .setMessage(R.string.workout_merge_list_message_back_to_select)
                .setPositiveButton(R.string.cancel, (DialogInterface.OnClickListener) null)
                .setNegativeButton(R.string.confirm, (dialog, which) -> WorkoutMergeListActivity.this.finish())
                .show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RecyclerViewItemClickHelper recyclerViewItemClickHelper = this.eventHelper;
        if (recyclerViewItemClickHelper != null) {
            recyclerViewItemClickHelper.destroy();
        }
        IWorkoutMergeListPresenter iWorkoutMergeListPresenter = this.presenter;
        if (iWorkoutMergeListPresenter != null) {
            iWorkoutMergeListPresenter.destroy();
        }
        this.presenter = null;
        this.eventHelper = null;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_MERGE_PREVIEW && resultCode == -1) {
            setResult(-1);
            finish();
        }
    }

    public static class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
        private WeakReference<IWorkoutMergeListPresenter> presenterRef;

        @Override
        public boolean isLongPressDragEnabled() {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        }

        public ItemTouchHelperCallback(IWorkoutMergeListPresenter presenter) {
            this.presenterRef = new WeakReference<>(presenter);
        }

        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            return makeMovementFlags(3, 0);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            IWorkoutMergeListPresenter iWorkoutMergeListPresenter = this.presenterRef.get();
            if (iWorkoutMergeListPresenter == null) {
                return false;
            }
            int adapterPosition = viewHolder.getAdapterPosition();
            int adapterPosition2 = target.getAdapterPosition();
            iWorkoutMergeListPresenter.move(adapterPosition, adapterPosition2);
            recyclerView.getAdapter().notifyItemMoved(adapterPosition, adapterPosition2);
            return true;
        }
    }
}
